package com.wgj.android.dialog;

import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiaai.xui.utils.XDateUtils;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.utils.XPreferencesUtils;
import com.wgj.android.R;
import com.wgj.android.entity.HBRecord;
import com.wgj.android.events.Adver;
import com.wgj.android.tools.MyJZVideoPlayerStandard;

import org.dom4j.io.XPP3Reader;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;

/**
 * Created by Administrator on 2018/2/3.
 */

public class AdverViedoDialog  extends AppCompatDialogFragment{
    public  static  AdverViedoDialog getInit(Adver adver){
         AdverViedoDialog adverViedoDialog=new AdverViedoDialog();
         Bundle bundle=new Bundle();
         bundle.putSerializable("adver",adver);
        adverViedoDialog.setArguments(bundle);
        return  adverViedoDialog;
    }

    @BindView(R.id.hongbaoView)
    View hongbaoView;
    @BindView(R.id.player_list_video)
    MyJZVideoPlayerStandard myJZVideoPlayerStandard;
    @BindView(R.id.hongBaoImg)
    ImageView hongBaoImg;
    @BindView(R.id.hongBaoTime)
    TextView hongBaoTime;


    private  Timer timer;
    private  Adver adver;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adver= (Adver) getArguments().getSerializable("adver");
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.adver_viedo_dialog ,container,false);
        ButterKnife.bind(this,view);
        initView();
        return view;
    }

    private void  initView(){

        myJZVideoPlayerStandard.setUp(adver.getViedoUrl(), JZVideoPlayer.SCREEN_WINDOW_LIST, adver.getTitle());
        myJZVideoPlayerStandard.setOnPlayCallback(new MyJZVideoPlayerStandard.OnPlayCallback() {
            @Override
            public void onStart() {
                if(adver.getIs_get()==0){
                        startTime();
                }
            }
        });
        Glide.with(getContext()).load(adver.getThumbnail()).into(myJZVideoPlayerStandard.thumbImageView);
    }

    private  void  startTime(){
        if(timer!=null){
            timer.cancel();
            timer=null;

        }
        timer=new Timer(15000,1000);
        timer.start();
        hongbaoView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.height = XDensityUtils.getScreenHeight();
        lp.width = XDensityUtils.getScreenWidth();
        window.setAttributes(lp);
    }

    @OnClick(R.id.closeBut)
    public  void  onCloseBut(View view){
        dismiss();
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        try {
            if(timer!=null){
                timer.cancel();
                timer=null;
            }
            myJZVideoPlayerStandard.releaseAllVideos();
        }catch (Exception e){

        }


    }

    class Timer extends CountDownTimer{


        public Timer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            hongBaoTime.setText(millisUntilFinished / 1000+"");

        }
        @Override
        public void onFinish() {
            hongbaoView.setVisibility(View.GONE);
            OpenAdverHongBaoDialog.getInstael(adver).show(getChildFragmentManager(),"OpenAdverHongBaoDialog");
        }
    }
}
